<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #canvas {
      box-shadow: 2px 4px 6px #eee;
    }
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
</body>
<script src="/script/utils.js"></script>
<script src="/components/Ball.js"></script> 
<script>
  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')
  
  const W = canvas.width = 800
  const H = canvas.height = 600

  // let mouse = C.getOffset(canvas)

  let balls = [], g = 0.05
  for (let i = 0; i < 100; i++) {
    balls.push(new Ball({
      x: W / 2,
      y: H,
      r: Math.random() > 0.9 ? C.rp([25, 40]) : C.rp([15, 30]),
      fillStyle: C.createColor(),
      vx: C.rp([-3, 3]),
      vy: C.rp([0, -10])
    }))
  }

  function drawBall(ball, index) {
    ball.x += ball.vx
    ball.y += ball.vy
    ball.vy += g

    if (ball.x - ball.r >= W || ball.x + ball.r <= 0 ||
    ball.y - ball.r >= H || ball.y + ball.r <= 0) {
      ball.x = W / 2
      ball.y = H
      ball.vx = C.rp([-3, 3])
      ball.vy = C.rp([0, -10])
    }

    ball.render(ctx)
  }

  function move() {
    window.requestAnimationFrame(move)

    ctx.clearRect(0, 0, W, H)

    let i = balls.length
    while (i--) {
      drawBall(balls[i], i)
    }
  }
  move()
</script>
</html>